package AICore.Engine;

import game.GameBoard;
import game.Player;

import java.util.LinkedList;

import carddeck.Card;
import carddeck.Rank;
import carddeck.Suit;

public abstract class AIEnginePlay extends AIEngineBase {
	public AIEnginePlay(AIEngineUnit engineUnit) {
		super(engineUnit);
	}

	public abstract void makeAMove(boolean bAfterdraw);
	
	public LinkedList<Suit> getWinningColorsFor(Player other, boolean skipJokers) {
		LinkedList<Suit> colors = new LinkedList<>();
		if(other.getMovesToWin()==1) {
			LinkedList<Card> oCards = other.getCards();
			for(Card theCard: oCards) {
				if(!theCard.isJoker()&&theCard.getCardRank()!=Rank.QUEEN) {
					colors.add(theCard.getCardSuit());
				}
				if(!skipJokers&&theCard.isJoker()) {
					colors.clear();
					return null;
				}
			}
		}
		else
			throw new UnsupportedOperationException("this function should be used only to players with 1 move to go!");
		return colors; // if colors is empty, next Player is full of queens, if it is null, next player has a joker
	}
	
	public int getKingOfSpadesCountersIn(LinkedList<Card> set) {
		int count = 0;
		for(Card theCard: set)
			if((theCard.getCardRank()==Rank.KING&&(theCard.getCardSuit()==Suit.HEARTS||theCard.getCardSuit()==Suit.SPADES))||theCard.isJoker()||((theCard.getCardRank()==Rank.TWO||theCard.getCardRank()==Rank.THREE)&&theCard.getCardSuit()==Suit.SPADES))
				count++;
		return count;
	}
	
	public Rank getWinningRankFor(Player other) {
		if(other.getMovesToWin()==1) {
			LinkedList<Card> oCards = other.getCards();
			for(Card theCard: oCards) {
				if(!theCard.isJoker())
					return theCard.getCardRank();
			}
		}
		else
			throw new UnsupportedOperationException("this function should be used only to players with 1 move to go!");
		return null; //just joker, or set of jokers! VAE VICTIS!
	}

	public Rank getDemandFor(Player other) {
		Rank demand = GameBoard.getInstance().getCardOnDemand();
		if(demand!=null) {
			Player dp = engineUnit.playerOwner;
			Player demandedBy = GameBoard.getInstance().getDemandingPlayer();
			while(dp!=other.nextPlayer) {
				if(demandedBy==dp)
					demand=null;
				dp=dp.nextPlayer;
			}
		}
		return demand;
	}
}
